//
//  ReadyTitleView.m
//  NewGame
//
//  Created by wwj on 15/10/12.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "ReadyTitleView.h"
#import "StageModel.h"
#import "SoundTool.h"

@interface ReadyTitleView ()
@property (nonatomic, weak) IBOutlet UILabel *stageNo;
@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labelArray;
@end

@implementation ReadyTitleView

+(instancetype)readyTitleView{
    return [[[NSBundle mainBundle] loadNibNamed:@"ReadyTitleView" owner:nil options:nil] firstObject];
}

-(void)setStageModel:(StageModel *)stageModel{
    _stageModel = stageModel;
    
    [[SoundTool sharedSoundTool] playSound:kSoundReady];
    
    _stageNo.text = [NSString stringWithFormat:@"Stage %d", stageModel.no];
    
    NSArray *titleArray = [_stageModel.title componentsSeparatedByString:@"\\n"];
    CGFloat sumTime;
    for (int index = 0; index < titleArray.count; index++) {
        UILabel *titleLable = _labelArray[index];
        titleLable.hidden = YES;
        titleLable.text = titleArray[index];
        titleLable.tag = index+1;
        CGFloat time = 0.3 * (index + 1);
        sumTime +=time;
        
        [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(showLable:) userInfo:titleLable repeats:NO];
    }
    
    [NSTimer scheduledTimerWithTimeInterval:sumTime target:self selector:@selector(deleteView) userInfo:nil repeats:NO];
    
}

-(void)deleteView{
    [self removeFromSuperview];
    if (_finishBlock) {
        _finishBlock();
    }
}

-(void)showLable:(NSTimer *)timer{
    UILabel *lable = timer.userInfo;
    [[SoundTool sharedSoundTool] playSound:kSoundDrop(lable.tag)];
    lable.hidden = NO;
    
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scale.fromValue = @0;
    scale.toValue = @1;
    
    CAKeyframeAnimation *translation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
    translation.values = @[@0, @40, @0];
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations =@[translation, scale];
    [lable.layer addAnimation:group forKey:nil];
}

@end
